package com.citycloud.ccuap.tc.yq.meatbustype;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.citycloud.ccuap.commons.constants.GlobalOperateType;
import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.openapi.controller.base.BaseController;
import com.citycloud.ccuap.commons.web.session.CurrentUser;
import com.citycloud.ccuap.commons.web.session.SessionConstant;
import com.citycloud.ccuap.commons.web.util.ResolveRequest;
import com.citycloud.ccuap.framework.mybatis.pagination.PageQuery;
import com.citycloud.ccuap.framework.util.StringUtil;
import com.citycloud.ccuap.tc.yq.entity.MetaBusType;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("业务类型")
@RestController
@RequestMapping("/v1/yq/meatbustype")
public class MeatBusTypeController extends BaseController {
	@Autowired
    private IMeatBusTypeService meatBusTypeService;
    
	
	/**
	 * 默认构造函数
	 */
	public MeatBusTypeController() {
	}

	@ApiOperation("根据主键获取对象")
	@RequestMapping(value = "/findById", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData findById() throws Exception {
		String objId = getStringParameter("objId");
		MetaBusType meatBusType = meatBusTypeService.findById(objId);
		
		JSONData jsonData = new JSONData(meatBusType);
		return jsonData;
	}
	
	/**
	 * 获取页面提交的对象
	 * 
	 * @return struts result
	 * @throws Exception
	 */
	private MetaBusType getRequestDataObject(short operateType) throws Exception {
		MetaBusType meatBusType = new MetaBusType();
		ResolveRequest.fillData(request, meatBusType);



		CurrentUser user = (CurrentUser) request.getSession().getAttribute(SessionConstant.CURRENT_USER_KEY);
		if (GlobalOperateType.ADD == operateType) {
		} else if (GlobalOperateType.EDIT == operateType) {
		}

		return meatBusType;
	}
	
	@ApiOperation("新增对象")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JSONData add() throws Exception {
		MetaBusType meatBusType = getRequestDataObject(GlobalOperateType.ADD);
		meatBusTypeService.insert(meatBusType);

		JSONData jsonData = new JSONData("添加业务类型成功！");
		return jsonData;
	}

	@ApiOperation("更新对象")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JSONData update() throws Exception {
		MetaBusType meatBusType = getRequestDataObject(GlobalOperateType.EDIT);
		meatBusTypeService.update(meatBusType);

		JSONData jsonData = new JSONData("修改业务类型成功！");
		return jsonData;
	}
	
	@ApiOperation("保存或更新对象，一般用于表格数据的编辑")
	@RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
	public JSONData saveAndUpdate() throws Exception {
		MetaBusType meatBusType = getRequestDataObject(GlobalOperateType.EDIT);
		meatBusTypeService.save(meatBusType);

		JSONData jsonData = new JSONData("修改业务类型成功！");
		return jsonData;
	}

	@ApiOperation("删除对象")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JSONData delete() throws Exception {
		String objId = getStringParameter("objId");
		meatBusTypeService.deleteById(objId);
		
		JSONData jsonData = new JSONData("删除业务类型成功！");
		return jsonData;
	}
	
	@ApiOperation("删除多个对象")
	@RequestMapping(value = "/deleteList", method = RequestMethod.POST)
	public JSONData deleteList() throws Exception {
		String objIds = getStringParameter("objIds");
		String formatTempates = getStringParameter("formatTempates");
		List<String> objIdList = StringUtil.strToStringList(objIds);
		
		meatBusTypeService.deleteIdList(objIdList);
		
		JSONData jsonData = new JSONData("删除业务类型成功！");
		return jsonData;
	}

	@ApiOperation("根据条件查询对象")
	@RequestMapping(value = "/query", method = { RequestMethod.GET, RequestMethod.POST })
	public JSONData query() throws Exception {
		PageQuery<MetaBusType> pageQuery = ResolveRequest.getPageForParams(request);
		MeatBusTypeDTO meatBusTypeDTO = new MeatBusTypeDTO();
		ResolveRequest.fillData(request, meatBusTypeDTO, "q_", true);

		Sort sort = Sort.by(Sort.Direction.DESC, "objId");

//		Sort sort = new Sort(DESC, "objId");
		pageQuery.setSort(sort);
		
		
		PageQuery<MetaBusType> pageData = meatBusTypeService.selectPage(pageQuery, meatBusTypeDTO);
		
		JSONData jsonData = new JSONData(pageData);
		return jsonData;
	}

}